from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib import auth
from django.core.context_processors import csrf
from django.shortcuts import redirect
from django.shortcuts import render
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required


#Import User Forms
from users.forms import SigninForm
from users.forms import SignupForm


def login(request):

	if request.method == "GET":
		signin_form = SigninForm()
		context = {'signin_form' : signin_form,}
		context.update(csrf(request))
		return render_to_response('users/login.html', context)

	elif request.method == "POST":
		form = SigninForm(request.POST)		# fill form with incoming post data

		if form.is_valid():		# if form is filled correctly
			form_data = form.cleaned_data	# create dictionary form data

			username = form_data['username']
			password = form_data['password']

			user = auth.authenticate(username=username, password=password)


			if user is not None:	# the password verified for the user
				if user.is_active:
					request.session['user_id'] = user.id
					request.session['user_name'] = user.username
				
					return redirect('/hotels/search/')
				else:
					signin_form = SigninForm()
					context = {'signin_form' : signin_form, 'error_message': 'Your Account Has Been DeActivated By The Admin!',}
					context.update(csrf(request))
					return render(request, 'users/login.html', context)
			else:	# the authentication system was unable to verify the username and password
				signin_form = SigninForm()
				context = {'signin_form' : signin_form, 'error_message': 'The Login Details Are Invalid!',}
				context.update(csrf(request))	
				return render(request, 'users/login.html', context)

		else:
			signin_form = SigninForm()
			context = {'signin_form' : signin_form, 'error_message': 'Please Enter Your Login Details'}
			context.update(csrf(request))
			return render_to_response('users/login.html', context)

@login_required
def logout(request):
	if 'user_id' not in request.session:
		return redirect('/users/login/')

	auth.logout(request)
	return redirect('/hotels/search/')


def signup(request):
	if request.method == "GET":
		signup_form = SignupForm()
		context = {'signup_form' : signup_form,}
		context.update(csrf(request))
		return render_to_response('users/signup.html', context)

	elif request.method == "POST":
		form = SignupForm(request.POST)		# fill form with incoming post data

		if form.is_valid():		# if form is filled correctly
			form_data = form.cleaned_data	# create dictionary form data

			if form_data['password'] == form_data['retpassword']:
				try:
					user = User.objects.create_user(form_data['username'], form_data['email'], form_data['password'])	
					return redirect('/users/login/')
				
				except:
					signup_form = SignupForm()
					context = {'signup_form' : signup_form, 'error_message': 'Error Happened Or User Name Already Exists Please Try Again!'}
					context.update(csrf(request))
					return render_to_response('users/signup.html', context)
				
		else:
			signup_form = SignupForm()
			context = {'signup_form' : signup_form, 'error_message': 'Please Check Your Details'}
			context.update(csrf(request))
			return render_to_response('users/signup.html', context)
